/* NOTICE
    OpenJaWS - Open Java Weather Station
    
    Copyright (C) 2008 Grant Gardner <grant@lastweekend.com.au>
    
    OpenJaWS is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
NOTICE */

package au.com.lastweekend.openjaws.api;

import au.com.lastweekend.openjaws.units.WindUnit;

/**
 * Adds "direction" to the a unit value of WindSpeed.
 * 
 * Direction is in degrees with methods to turn that into 32 point compass values.
 * 
 * @author ggardner
 * 
 */
public class WindUnitValue extends UnitValue {

    // TODO: Localise
    private static final String[] COMPASS_ROSE = {"N", "NbE", "NNE", "NEbN", "NE", "NEbE", "ENE", "EbN", "E", "EbS", "ESE",
            "SEbE", "SE", "SEbS", "SSE", "SbE", "S", "SbW", "SSW", "SWbS", "SW", "SWbW", "WSW", "WbS", "W", "WbN", "WNW", "NWbW",
            "NW", "NWbN", "NNW", "NbW"};

    public static final WindUnitValue NULL = new WindUnitValue();

    private Double direction; // degrees.

    protected WindUnitValue() {

        super();

    }

    public WindUnitValue(double currentValue, WindUnit unit) {

        this(currentValue, unit, null);
    }

    public WindUnitValue(double currentValue, WindUnit unit, double direction) {

        this(currentValue, unit, Double.valueOf(direction));
    }

    private WindUnitValue(double currentValue, WindUnit unit, Double direction) {

        super(currentValue, unit);
        this.direction = direction;
    }

    public Double getDirection() {

        return direction;
    }

    public String getCompassDirection() {

        if (direction == null) {
            return "";
        }

        return COMPASS_ROSE[(int) (Math.round(direction / 11.25) % 32)];
    }

    @Override
    /**
     * We are only comparing the wind speed. Can't compare greater/less than direction.
     */
    public int compareTo(UnitValue o) {

        return super.compareTo(o);

    }

    /**
     * @param to
     * @return converted WindUnitValue
     * 
     */
    public WindUnitValue convertTo(WindUnit to) {

        if (to == null || getValue() == null || getUnit() == null) {
            return NULL;
        }

        return new WindUnitValue(to.convertFrom(getValue(), getUnit()), to, direction);
    }

    @Override
    public int hashCode() {

        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((direction == null) ? 0 : direction.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {

        if (this == obj)
            return true;
        if (!super.equals(obj))
            return false;
        if (getClass() != obj.getClass())
            return false;
        final WindUnitValue other = (WindUnitValue) obj;
        if (direction == null) {
            if (other.direction != null)
                return false;
        } else if (!direction.equals(other.direction))
            return false;
        return true;
    }

    @Override
    protected String getFormatNonValuePart() {

        String compassDirection = getCompassDirection();
        if (direction == null) {
            compassDirection = "-";
        }
        return String.format("%s %s", getUnit(), compassDirection);
    }
}
